#include "Transaction.hh"
#include "Utilities.hh"
#include "main.hh"
#include <iomanip>
#include <sstream>
#include <time.h>


Transaction::Transaction(double amt, const std::string& typ, const std::string& n)
    : transactionId(generateTransactionId()), timestamp(std::time(nullptr)), 
      amount(amt), type(typ), note(n) {}

std::string Transaction::getTransactionId() const {
    return transactionId;
}

std::time_t Transaction::getTimestamp() const {
    return timestamp;
}

double Transaction::getAmount() const {
    return amount;
}

std::string Transaction::getType() const {
    return type;
}

std::string Transaction::getNote() const {
    return note;
}

std::string Transaction::generateTransactionId() {
    static int counter = 0;
    std::stringstream ss;
    ss << "T" << std::setw(3) << std::setfill('0') << ++counter;
    return ss.str();
}

std::ostream& operator<<(std::ostream& os, const Transaction& t) {
    char buffer[80];
    std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&t.timestamp));
    
    os << "[" << buffer << "] "
       << "ID: " << t.transactionId << " | "
       << "Type: " << t.type << " | "
       << "Amount: " << Utilities::formatCurrency(t.amount) << " | "
       << "Note: " << (t.note.empty() ? "N/A" : t.note);
    
    return os;
}